// component/ui/z-search/z-search.js
import * as transferData from '../../../models/transferData'

Component({
  /**
   * 组件的配置
   */
  options: {
    styleIsolation: 'isolated',
    pureDataPattern: /^_/
  },

  /**
   * 组件的属性列表
   */
  properties: {
    border: {
      type: Boolean,
      value: false,
    },
    value: {
      type: String,
      value: '',
    },
    history: {
      type: String,
      value: '',
    },
    accurate: { //动态接口
      type: String,
      value: '',
    },
    accurateType: {
      type: String,
      value: 'id',
    },
    placeholder: {
      type: String,
      value: '搜索',
    },
    cityName: {
      type: String,
      value: '',
    },
    cityCode: {
      type: String,
      value: '',
    },
    hideCount: {
      type: Boolean,
      value: false,
    },
    name: {
      type: String,
      value: 'search',
    },
    vague: {
      type: Boolean,
      value: true,
    },
    isListShow:{ //判断是哪里使用
      type: Boolean,
      value: '',
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    current: '',
  },

  /**
   * 组件的数据监听
   */
  observers: {
    value(val) {
      this.setData({
        current: String(val)
      })
    }
  },

  /**
   * 组件的生命周期
   */
  lifetimes: {
    attached: function () {},
    detached: function () {},
  },

  /**
   * 页面的生命周期
   */
  pageLifetimes: {
    show: function () {
      this.updateSearch()
    },
    hide: function () {},
    resize: function (size) {}
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onSearch() {
      const {
        history,
        accurate,
        placeholder,
        cityCode,
        cityName,
        value,
        accurateType,
        hideCount,
        name,
        vague,
        isListShow
      } = this.data

      wx.navigateTo({
        url: `/pages/home/search/search?search=${value}&history=${history}&accurate=${accurate}&placeholder=${placeholder}&citycode=${cityCode === 'all' ? '' : cityCode}&cityname=${cityName === '全国' ? '' : cityName}&accuratetype=${accurateType}&count=${hideCount ? '1' : '0'}&name=${name}&vague=${vague ? '1' : '0'}&isListShow=${isListShow}`,
      })
    },

    onClear() {
      this.triggerEvent('change', {
        label: '',
        value: '',
        type: ''
      })
      this.setData({
        current: ''
      })
    },

    updateSearch() {
      const {
        name
      } = this.data
      let data = transferData.getTemporaryData(name)
      if (data) {
        transferData.resetTemporaryData(name)
        this.setData({
          current: data.label
        })
        this.triggerEvent('change', data)
      }
    },

  }
})